local buzun = fk.CreateSkill{
  name = "rfenghou_3d18j__buzun",
  max_turn_use_time = 1,
}

buzun:addEffect(fk.FinishJudge, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if target and target ~= player and player:hasSkill(buzun.name) then
      return data.card and player.room:getCardArea(data.card) == Card.Processing
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local arg = data.card:toLogString()
    local trigger = player:usedSkillTimes(buzun.name, Player.HistoryRound) == 0
    if trigger then
      arg = arg .. Fk:translate("#rfenghou_3d18j__buzun-after")
    end
    if room:askToSkillInvoke(target, {
      skill_name = buzun.name,
      prompt = "#rfenghou_3d18j__buzun-invoke::" .. player.id .. ":" .. arg,
    }) then
      event:setCostData(self, { succeed = trigger })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local dat = event:getCostData(self)
    room:obtainCard(player, data.card, false, fk.ReasonPrey, target, buzun.name)
    if not dat.succeed then return end
    local armor = {}
    local weapons = {}
    local max_range
    for _, to in ipairs(room:getOtherPlayers(player, false)) do
      for _, cid in ipairs(to:getCardIds("e")) do
        local card = Fk:getCardById(cid, true)
        if card.sub_type == Card.SubtypeWeapon then
          ---@cast card Weapon
          local range = card:getAttackRange(to)
          if not max_range or range > max_range then
            max_range = range
          end
          weapons[range] = weapons[range] or {}
          table.insert(weapons[range], {to, cid})
        elseif card.sub_type == Card.SubtypeArmor then
          table.insert(armor, {to, cid})
        end
      end
    end
    local suitable_pairs = table.simpleClone(weapons[max_range] or {})
    if #armor == 1 then
      table.insert(suitable_pairs, armor[1])
    end
    if #suitable_pairs == 0 then return end
    local suitable_member, suitable_cards = {}, {}
    for _, pair in ipairs(suitable_pairs) do
      local to, card = pair[1], pair[2]
      table.insertIfNeed(suitable_member, to)
      suitable_cards[to] = suitable_cards[to] or {}
      table.insertIfNeed(suitable_cards[to], card)
    end
    local choice_to
    repeat
      choice_to = room:askToChoosePlayers(player, {
        min_num = 1,
        max_num = 1,
        targets = suitable_member,
        skill_name = buzun.name,
        prompt = "#rfenghou_3d18j__buzun-use",
      })[1]
      if not choice_to then return end
    until room:askToUseRealCard(player, {
      pattern = suitable_cards[choice_to],
      expand_pile = suitable_cards[choice_to],
      skill_name = buzun.name,
      prompt = "#rfenghou_3d18j__buzun-use",
    })
  end
}, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["rfenghou_3d18j__buzun"] = "卜尊",
  [":rfenghou_3d18j__buzun"] = "每回合限一次，其他角色的判定牌生效后，其可以令你获得之。若本轮未发动过此技能，你可以使用场上攻击范围最大的武器牌或场上唯一的防具牌。",

  ["#rfenghou_3d18j__buzun-invoke"] = "卜尊：你可以令 %dest 获得 %arg",
  ["#rfenghou_3d18j__buzun-after"] = "，其可以使用场上的武器牌或防具牌",

  ["#rfenghou_3d18j__buzun-use"] = "卜尊：你可以使用场上攻击范围最大的武器牌或场上唯一的防具牌",
}

return buzun
